﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mortgage.DAL.Entity;
using Mortgage.DAL;
using Mortgage.Srv.Cls;

namespace Mortgage.Srv
{
    public interface IBillSrv
    {
        List<tBill> GetByMortgageId(long mortgageId);
        int Insert(tBill insObj, EnmMortgageStatus morgateStatus);
        int Update(tBill newsObj, EnmMortgageStatus morgateStatus);
        int Delete(tBill delObje);
        tBill GetById(long id);
        DateTime GetDateBeginMortgage(long mortId, DateTime dte);
    }

    class BillSrv : IBillSrv
    {
        private MortgageDataContext dataContext;

        public BillSrv()
        {
            dataContext = new MortgageDataContext();

        }

        public tBill GetById(long id)
        {
            return dataContext.tBills.Where(b => b.ID == id).FirstOrDefault();
        }

        public List<tBill> GetByMortgageId(long mortgageId)
        {
            return dataContext.tBills.Where(m => m.MorID == mortgageId && !m.IsDelete.GetValueOrDefault(true)).OrderByDescending(m => m.ModifiedDate).ToList<tBill>();
        }

        public int Insert(tBill insObj, EnmMortgageStatus morgateStatus)
        {
            int result = 0;

            tMortgage mortgage = dataContext.tMortgages.Where(m => m.ID == insObj.MorID).FirstOrDefault();
            if (mortgage == null)
                return 3;
            insObj.IsDelete = false;
            dataContext.tBills.InsertOnSubmit(insObj);
            mortgage.Status = (int)morgateStatus;

            mortgage.ModifiedBy = insObj.CreatedBy;
            mortgage.ModifiedDate = insObj.CreatedDate;


            dataContext.SubmitChanges();

            return result;
        }

        public int Update(tBill newsObj, EnmMortgageStatus morgateStatus)
        {
            int result = 0;

            tMortgage mortgage = dataContext.tMortgages.Where(m => m.ID == newsObj.MorID).FirstOrDefault();
            if (mortgage == null)
                return 3;

            if (!dataContext.tBills.Any(m => m.ID == newsObj.ID))
                return 1;

            tBill itm = dataContext.tBills.Where(m => m.ID == newsObj.ID).FirstOrDefault();

            itm.MoneyAmount = newsObj.MoneyAmount;
            itm.BillDate = newsObj.BillDate;
            itm.BillDescription = newsObj.BillDescription;
            itm.BillType = newsObj.BillType;

            itm.ModifiedBy = newsObj.ModifiedBy;
            itm.ModifiedDate = newsObj.ModifiedDate;

            mortgage.Status = (int)morgateStatus;
            mortgage.ModifiedBy = itm.ModifiedBy;
            mortgage.ModifiedDate = itm.ModifiedDate;

            dataContext.SubmitChanges();
            return result;
        }

        public int Delete(tBill delObje)
        {
            int result = 0;

            if (!dataContext.tBills.Any(m => m.ID == delObje.ID))
                return 1;

            tBill itm = dataContext.tBills.Where(m => m.ID == delObje.ID).FirstOrDefault();

            itm.IsDelete = true;
            itm.ModifiedBy = delObje.ModifiedBy;
            itm.ModifiedDate = delObje.ModifiedDate;

            dataContext.SubmitChanges();
            return result;
        }

        public DateTime GetDateBeginMortgage(long mortId, DateTime dte)
        {
            List<DateTime> dteLst = (from m in dataContext.tMortgages
                                     where m.ID == mortId
                                     select m.MorDate.GetValueOrDefault())
                                     .Union(from b in dataContext.tBills
                                            where b.MorID == mortId
                                            && b.BillDate < dte
                                            select b.BillDate.GetValueOrDefault()).OrderBy(m => m).ToList();

            DateTime result = DateTime.Now;
            if (dteLst.Count > 0)
                result = dteLst[dteLst.Count - 1];


            return result;
        }

    }
}
